<template>
  <div class="cate-product">
    <div class="container">
      <div class="local">
        <img src="~/assets/images/local.png" alt="" />
        当前位置 :
        <router-link to="/category" v-if="list">包装材料</router-link>
        <img src="@/assets/images/home/icon.png" alt="" />
        {{ cate_name }}
      </div>

      <!-- 商品属性 -->
      <cate-details :id="id" :showTab="true" />

      <!--相似推荐-->
      <recommend class="recommend" :list="list" @changeProduct="seeProductDe" />

      <div class="guess" ref="guess">
        <div class="title"><h2>猜你需要</h2></div>
        <div class="content">
          <div class="ser-item" v-for="(item, index) in likeList" :key="index">
            <div class="pic-box">
              <img :src="item.image" alt="" class="object" />
              <i class="collect"><div class="heart" /></i>
            </div>
            <div class="ser-desc">
              <h3>{{ item.store_name }}</h3>
              <p class="pro-number">
                <span>产品编号：</span>
                {{ item.product_code || '无' }}
              </p>
              <p class="pro-info por-info-text">{{ item.desc }}</p>
              <div class="num">
                <label>
                  数量:
                  <input type="text" v-model="item.moq" />
                </label>
                Pcs
              </div>
              <div class="btn">
                <a class="into-cart cursor" @click="addShopCart(item)">
                  <img src="~/assets/images/whitecart.png" alt="" />
                  加入购物车
                </a>
                <a class="ask-price cursor" @click="goInquiry(item)">发起询价</a>
              </div>
            </div>
            <div class="pro-info">
              <div class="table-box" v-if="item.zhekou.ladder_json">
                <table border="0" cellpadding="0" cellspacing="0">
                  <thead>
                    <tr>
                      <th>数量</th>
                      <th>
                        价格
                        <span>（原价/折扣价）</span>
                      </th>
                      <th>交期</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr v-for="(zk, index) in item.zhekou.ladder_json" :key="index">
                      <td>{{ zk.num }} - {{ zk.max_num }}</td>
                      <td class="price">
                        <span>{{ zk.price }}</span>
                        /{{ zk.discounts || 0 }}
                      </td>
                      <td>{{ zk.days }}天</td>
                    </tr>
                  </tbody>
                </table>
              </div>
              <div class="pro-features clearfix">
                <div class="feature">
                  <!-- v-if="info.pro_ste && info.pro_ste[0] && info.pro_ste[0] != 0" -->
                  <span>
                    <img src="~/assets/images/icon1.png" alt="" />
                    <span>免费样品</span>
                  </span>
                </div>
                <div class="feature">
                  <!-- v-if="info.pro_ste && info.pro_ste[1] && info.pro_ste[1] != 0" -->
                  <span>
                    <img src="~/assets/images/icon2.png" alt="" />
                    库存有现货
                  </span>
                </div>
                <div class="feature">
                  <!-- v-if="info.pro_ste && info.pro_ste[2] && info.pro_ste[2] != 0" -->
                  <span>
                    <img src="~/assets/images/icon3.png" alt="" />
                    支持PCR
                  </span>
                </div>
                <div class="feature">
                  <!-- v-if="info.pro_ste && info.pro_ste[3] && info.pro_ste[3] != 0" -->
                  <span>
                    <img src="~/assets/images/icon4.png" alt="" />
                    支持定制
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <m-pagination :pagination="total" :page-size="limit" @changePage="changeThisPage" />
      
    </div>
  </div>
</template>

<script>
import CateDetails from '@/components/CateDetails';
import Recommend from '@/components/Recommend';
import { getRecommend, getGuess, addCart } from '@/request/api';

export default {
  name: 'CateProductDetails',
  components: {
    CateDetails,
    Recommend
  },
  data() {
    return {
      id: '',
      info: {},
      cate_name: localStorage.getItem('cate_name'),
      list: [],
      likeList: [],
      detailsInfo: {},
      page: 1,
      limit: 10,
      total: 0
    };
  },
  created() {
    this.id = this.$route.params.id;
    this.recommend();
    this.guess();
  },
  methods: {
    // 相似推荐
    // todo 相似推荐接口要改，接口返回数据不对
    recommend() {
      const data = {
        page: 1,
        limit: 10,
        id: this.id
      };
      getRecommend(data)
        .then(res => {
          console.log(res,'res');
          this.list = res.data.data || [];
          // console.log(this.list,'this.list')
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    //  猜你需要
    guess() {
      let that = this;
      let data = {
        page: this.page,
        limit: this.limit,
        id: this.id
      };
      this.likeList = [];
      getGuess(data)
        .then(res => {
          that.total = res.data.total;
          const data = res.data.data || [];
          for (const i in data) {
            data[i].pro_ste = data[i].pro_ste.split('');
          }
          that.likeList = data;
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    changeThisPage(page) {
      window.scrollTo({ top: 1600 });
      this.page = page;
      this.guess();
    },
    seeProductDe(id) {
      console.log(1);
      this.$router.push({
        name: 'CateProduct',
        params: {
          id
        },
        query: {
          t: Date.now()
        }
      });
    },
    // 加入购物车
    addShopCart(item) {
      if (item.moq <= 0) {
        this.$message.error('请输入正确的数量！');
        return;
      }
      let data = {
        cart_num: item.moq,
        is_new: 0,
        product_id: item.product_id,
        product_type: 0
      };
      this.$toastVant.loading('请稍后');
      addCart(data)
        .then(res => {
          if (res.status === 200) {
            this.$toastVant.success('加入成功！');
          }
        })
        .catch(err => {
          this.$message.error(err.message);
        });
    },
    // 发起询价
    goInquiry(item) {
      this.$router.push({
        name: 'CateInquiry',
        query: { mid: item.mer_id, proId: item.product_id }
      });
    }
  }
};
</script>

<style scoped>
.cate-product {
  background: #f3f3f7;
  padding: 30px 0 50px;
}

.local {
  color: #656565;
  margin-bottom: 30px;
  display: flex;
  align-items: center;
}

.local img {
  margin-right: 10px;
}

.shop-desc p {
  margin-bottom: 20px;
}

.recommend {
  margin: 30px 0 20px;
}

.guess .title {
  position: relative;
  font-size: 20px;
  color: #4d4d4d;
  background: #f3f3f7;
  z-index: 100;
}

.guess .title h2 {
  background: #f3f3f7;
  padding-right: 20px;
}

.guess .content {
  padding-top: 30px;
}

.ser-item {
  display: flex;
  align-items: flex-start;
  padding: 30px 28px;
  color: #666;
  background: #fff;
  border: 1px solid #e9e9e9;
  font-size: 16px;
  margin-bottom: 16px;
}

.ser-item .pic-box {
  width: 356px;
  height: 303px;
  position: relative;
}

.ser-item .pic-box .collect {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 33px;
  height: 33px;
  background: #fff;
  opacity: 0.28;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.ser-desc {
  width: 400px;
  flex: 2;
  margin: 0 30px;
}

.ser-desc h3 {
  font-size: 20px;
  font-weight: bold;
  color: #4d4d4d;
}

.ser-desc p {
  margin-top: 18px;
}

.ser-desc .pro-info {
  font-size: 14px;
  color: #969696;
}

.num {
  margin: 23px 0;
}

.num input {
  width: 220px;
  height: 35px;
  border: 1px solid rgb(101, 101, 101);
  border-radius: 10px;
  margin: 3px 10px;
  padding: 0 10px;
}

.btn a {
  padding: 10px 18px;
  color: #fff;
  background: rgb(227, 139, 43);
  border-radius: 10px;
  margin-right: 14px;
}

.btn a:hover {
  opacity: 0.8;
}

.into-cart img {
  vertical-align: middle;
  margin-right: 5px;
}

.btn .ask-price {
  color: #e38b2b;
  background: #fff;
  border: 1px solid rgb(227, 139, 43);
}

.pro-info {
  width: 462px;
}

.por-info-text {
  width: 400px;
}

.pro-info table {
  margin-top: 0;
}

.pro-info thead {
  color: #fff;
  background: #666;
  height: 42px;
  line-height: 42px;
  font-size: 16px;
}

.pro-info thead th {
  padding: 0 0;
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  user-select: none;
  border: 1px solid #666;
}

.pro-info tbody {
  background: #f3f4f7;
}

.pro-info tbody td {
  padding: 14px 0 14px;
  text-align: center;
}

.pro-info tbody td.price span {
  color: #969696;
  text-decoration: line-through;
}

.pro-info .pro-features {
  margin-top: 25px;
  font-size: 13px;
  color: #959595;
  display: flex;
}

.pro-info .pro-features img {
  width: 22px;
  height: 22px;
}

.pro-info .pro-features .feature {
  flex: 1;
  display: flex;
  justify-content: center;
}

.pro-info .pro-features .feature img {
  margin-right: 0;
  vertical-align: middle;
}

.pro-info .pro-features .feature span {
  line-height: 18px;
}

.title h2 {
  padding-left: 16px;
}

.title h2::before {
  content: '';
  position: absolute;
  top: 4px;
  left: 0;
  width: 5px;
  height: 20px;
  background: #797979;
  border-radius: 3px;
}

.wink:hover {
  color: #e38b2b;
}

.heart {
  width: 20px;
  height: 20px;
  background-size: 50px;
  background: url('../../assets/images/shoucanglogo.png') -4px -2px;
}

table {
  width: 100%;
  margin-top: 30px;
  font-size: 16px;
  border-bottom: 1px solid #ccc;
  border-right: 1px solid #ccc;
}

td,
th {
  padding: 16px 0 12px;
  text-align: center;
  border-top: 1px solid #ccc;
  border-left: 1px solid #ccc;
}
</style>
